﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Func : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("求两数之和:" + MyMath.Sum(1, 2)+"<br>");
        Response.Write("求多数之和:" + MyMath.Sum(1, 2, 3, 4, 5) + "<br>");
        Response.Write("求1+1/2+2/3+...99/100的和:" + MyMath.Sum() + "<br>");
        Response.Write("斐波那契数列第6项为:" + MyMath.F1(6) + "<br>");
        Response.Write("斐波那契数列第10项为:" + MyMath.Fib(10) + "<br>");

        int n = MyMath.GetMin(6, 8);
        Response.Write("6和8的最小公倍数为:" + n.ToString()+"<br>");

        int[] number = { 1, 23, 3, 5, -1,100,6,0,10,-2 };
        int? maxIndex = null;//ref 必须要有初始值
        int? max = MyMath.Max(number, ref maxIndex);
        Response.Write("最大值:" + max +",索引为"+maxIndex+ "<br>");

        int? minIndex ;//out 可以没有初始值
        int? min = MyMath.Min(number, out  minIndex);
        Response.Write("最小值:" + min + ",索引为" + minIndex + "<br>");
        
        
    }
}

public class MyMath
{   /// <summary>
    /// 求两个整数之和
    /// </summary>
    /// <param name="a">操作数一</param>
    /// <param name="b">操作数二</param>
    /// <returns>两数之和</returns>
    public static int Sum(int a,int b)
    {
        return a + b;
    }

    /// <summary>
    /// 求多个数的和
    /// </summary>
    /// <param name="values">操作数集合(0个或多个)</param>
    /// <returns>多数之和,可为空</returns>
    public static int? Sum(params int[] values)
    {
        int? result = null;
        if (values.Length > 0)
        {
           result = 0;
           foreach(var value in values)
            {
                result += value;
           }
        }
        return result;
    }

    /// <summary>
    /// 求1+1/2+2/3+...99/100的和
    /// </summary>
    /// <returns>返回结果</returns>
    public static double? Sum()
    {
        double? sum=1.0;
        int i = 2;
        //for (int i = 2; i <= 100; i++)
        //{
        //    sum += Convert.ToDouble(i - 1) / Convert.ToDouble(i);
        //}
        //while (i <= 100)
        //{
        //    sum += Convert.ToDouble(i - 1) / Convert.ToDouble(i);
        //    i++;
        //}
        do
        {
            sum += Convert.ToDouble(i - 1) / Convert.ToDouble(i);
            i++;
        } while (i <= 100);
        return sum;
    }

    /// <summary>
    /// 递归求斐波那契数列第n项
    /// </summary>
    /// <param name="n">数列第n项</param>
    /// <returns>返回第n项的值</returns>
    public static int? F1(int n)
    {
        if (n == 1 || n == 2)
            return 1;
        else
            return F1(n - 1) + F1(n - 2);
    }

    /// <summary>
    /// 非递归求斐波那契数列第n项
    /// </summary>
    /// <param name="n">数列第n项</param>
    /// <returns>返回第n项的值</returns>
    public static int? Fib(int n)
    {
        if (n == 0)
        {
            return 0;
        }
        else if (n == 1)
        {
            return 1;
        }
        int n1 = 0;
        int n2 = 1;
        for (int i = 2; i <= n; i++)
        {
            int tmp = n1 + n2;
            n1 = n2;
            n2 = tmp;
        }
        return n2;
    }

    /// <summary>
    /// 求一组数中的最大值
    /// </summary>
    /// <param name="values">数据集合</param>
    /// <param name="maxIndex">返回最大值索引</param>
    /// <returns>最大值</returns>
    public static int? Max(int[] values, ref int? maxIndex)
    {
        if (values.Length <= 0)
        {
            maxIndex = null;
            return null;
        }
        else
        {
            var max = values[0];
            maxIndex = 0;
            for(int i = 0; i < values.Length; i++)
            {
                if (values[i] > max)
                {
                    max = values[i];
                    maxIndex = i;
                }
            }
            return max;
        }
    }

    /// <summary>
    /// 求一组数中的最小值
    /// </summary>
    /// <param name="values">数据集合</param>
    /// <param name="minIndex">返回最小值索引</param>
    /// <returns>最大值</returns>
    public static int? Min(int[] values,out int? minIndex)
    {
        if (values.Length <= 0)
        {
            minIndex = null;
            return null;
        }
        else
        {
            var min = values[0];
            minIndex = 0;
            for(int i = 0; i < values.Length; i++)
            {
                if (values[i]< min)
                {
                    min = values[i];
                    minIndex = i;
                }
            }
            return min;
        }
    }

    /// <summary>
    /// //最大公约数 
    /// </summary>
    public static int GetMax(int a, int b)
    {
        if (a<b)
        {
            a = a + b;
            b = a - b;
            a = a - b;
        }
        return (a % b == 0) ? b : GetMax(a % b, b);
    }
 
    /// <summary>
    /// //最小公倍数 
    /// </summary>
    public static int GetMin(int a, int b)
    {
        return a* b / GetMax(a, b);
    }

}

